//
//  XFNaviView.m
//  WorkManager
//
//  Created by ukongm on 17/1/5.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFNaviView.h"

/** 边距 */
static CGFloat const VIEW_GAP = 10;
/** 按钮的大小 */
static CGFloat const BUTTON_SIZE = 30;

@implementation XFNaviView

@synthesize titleView = _titleView;
@synthesize leftBtn = _leftBtn;
@synthesize rightBtn = _rightBtn;

- (UIView *)titleView{
    
    if (_titleView == nil) {
        CGFloat y = 20;
        
        UILabel* label;
        
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, y, 200, self.height - y)];
        
        label.center = CGPointMake(self.width/2, label.center.y);
        
        label.textColor = [UIColor whiteColor];
        
        label.font = [UIFont systemFontOfSize:16];
        
        label.textAlignment = NSTextAlignmentCenter;
        
        _titleView = label;
    }
    return _titleView;
}

- (UIButton *)leftBtn{
    
    if (_leftBtn == nil) {
        _leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(VIEW_GAP, 0, BUTTON_SIZE, BUTTON_SIZE)];
        
        _leftBtn.center = CGPointMake(_leftBtn.center.x, self.titleView.center.y);
    }
    return _leftBtn;
}

- (UIButton *)rightBtn{
    
    if (_rightBtn == nil) {
        
        CGFloat width = BUTTON_SIZE;
        
        _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.width - width - VIEW_GAP), 0, width, BUTTON_SIZE)];
        
        _rightBtn.center = CGPointMake(_rightBtn.center.x, self.titleView.center.y);
    }
    return _rightBtn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor naviColor];
        
        [self addSubview:self.titleView];
        [self addSubview:self.leftBtn];
        [self addSubview:self.rightBtn];
    }
    return self;
}

- (void)setLeftBtn:(UIButton *)leftBtn{
    
    _leftBtn = leftBtn;
    
    [self addSubview:leftBtn];
}

- (void)setRightBtn:(UIButton *)rightBtn{
    
    _rightBtn = rightBtn;
    
    [self addSubview:rightBtn];
}

- (void)setTitleView:(UIView *)titleView{
    
    _titleView = titleView;
    
    [self addSubview:titleView];
}

- (void)setTitleStr:(NSString *)title{
    if ([self.titleView isKindOfClass:[UILabel class]]) {
        
        UILabel* label = (UILabel*)self.titleView;
        
        label.text = title;
    }
}

@end
